package hms.payment;
import java.awt.*;
import java.awt.event.*;
import hms.FrontDeskModule.*;
import javax.swing.JDialog;
import hms.master.*;
import hms.database.*;
import javax.swing.*;
import java.sql.ResultSet;
import java.sql.SQLException; 
/**
 * <h2>Class Overview</h2> 
 * Class used to let front desk employee select payment method 
 * when doing check-out process in front desk
 * 
 * @author Yifeng Zou
 */

public class payMethod {
	private String type;
	public payMethod(final int reservationID) {
        final JDialog dialog = new JDialog();
        
        // implement ItemListener interface
        class MyItemListener implements ItemListener {
        	private int payMethodType=1; //default pay by cash
        	private int selectionResult;
            public void itemStateChanged(ItemEvent ev) {
                boolean selected = (ev.getStateChange() == ItemEvent.SELECTED);
                AbstractButton button = (AbstractButton) ev.getItemSelectable();
                String command = button.getActionCommand();
                if (selected) {
                    int messageType = -1;
                    String message = "";
                    if (command.equals("cash")) {
                        messageType = JOptionPane.INFORMATION_MESSAGE;
                        message = "Pay by Cash";
                        payMethodType=1;
                    } else if (command.equals("credit")) {
                        messageType = JOptionPane.INFORMATION_MESSAGE;
                        message = "Pay by Credit Card";
                        payMethodType=2;
                    } else if (command.equals("debit")) {
                        messageType = JOptionPane.INFORMATION_MESSAGE;
                        message = "Pay by Debit Card";
                        payMethodType=3;
                    } else if (command.equals("check")) {
                        messageType = JOptionPane.INFORMATION_MESSAGE;
                        message = "Pay by Check";
                        payMethodType=4;
                    }
                    
                    // show message
                    selectionResult=JOptionPane.showConfirmDialog(dialog,
                            		message,
                            		"Message Dialog",
                            		0,
                            		messageType);
                    DAO localDAO = MasterController.getDatabaseObject();
                    ResultSet rs = localDAO.getResult("select* from RESERVATION where RESERVATION_ID="+reservationID+";");
                    int gID=-1;
                    try {
            			while(rs.next()){
            				 gID=rs.getInt("PAYING_CUSTOMER_ID"); 
            				 //System.out.println("gID is: "+gID);
            			}
            		} catch (SQLException e) {
            			e.printStackTrace();
            		}
                    
                    if (selectionResult == JOptionPane.OK_OPTION && payMethodType==1 && gID!=-1){
                    	localDAO.updateCommand("UPDATE PAYMENT_METHOD SET CASH_SELECTED='TRUE' WHERE GUEST_ID="+gID+";");
                    	localDAO.updateCommand("UPDATE PAYMENT_METHOD SET CREDIT_SELECTED='FALSE' WHERE GUEST_ID="+gID+";");
                    	localDAO.updateCommand("UPDATE PAYMENT_METHOD SET DEBIT_SELECTED='FALSE' WHERE GUEST_ID="+gID+";");
                    	localDAO.updateCommand("UPDATE PAYMENT_METHOD SET CHECK_SELECTED='FALSE' WHERE GUEST_ID="+gID+";");
                    }
                    	
                    else if (selectionResult == JOptionPane.OK_OPTION && payMethodType==2 && gID!=-1){
                    	localDAO.updateCommand("UPDATE PAYMENT_METHOD SET CASH_SELECTED='FALSE' WHERE GUEST_ID="+gID+";");
                    	localDAO.updateCommand("UPDATE PAYMENT_METHOD SET CREDIT_SELECTED='TRUE' WHERE GUEST_ID="+gID+";");
                    	localDAO.updateCommand("UPDATE PAYMENT_METHOD SET DEBIT_SELECTED='FALSE' WHERE GUEST_ID="+gID+";");
                    	localDAO.updateCommand("UPDATE PAYMENT_METHOD SET CHECK_SELECTED='FALSE' WHERE GUEST_ID="+gID+";");	
                    }
                    	
                    	
                    else if (selectionResult == JOptionPane.OK_OPTION && payMethodType==3 && gID!=-1){
                    	localDAO.updateCommand("UPDATE PAYMENT_METHOD SET CASH_SELECTED='FALSE' WHERE GUEST_ID="+gID+";");
                    	localDAO.updateCommand("UPDATE PAYMENT_METHOD SET CREDIT_SELECTED='FALSE' WHERE GUEST_ID="+gID+";");
                    	localDAO.updateCommand("UPDATE PAYMENT_METHOD SET DEBIT_SELECTED='TRUE' WHERE GUEST_ID="+gID+";");
                    	localDAO.updateCommand("UPDATE PAYMENT_METHOD SET CHECK_SELECTED='FALSE' WHERE GUEST_ID="+gID+";");
                    }
                    	
                    else if (selectionResult == JOptionPane.OK_OPTION && payMethodType==4 && gID!=-1){
                    	localDAO.updateCommand("UPDATE PAYMENT_METHOD SET CASH_SELECTED='FALSE' WHERE GUEST_ID="+gID+";");
                    	localDAO.updateCommand("UPDATE PAYMENT_METHOD SET CREDIT_SELECTED='FALSE' WHERE GUEST_ID="+gID+";");
                    	localDAO.updateCommand("UPDATE PAYMENT_METHOD SET DEBIT_SELECTED='FALSE' WHERE GUEST_ID="+gID+";");
                    	localDAO.updateCommand("UPDATE PAYMENT_METHOD SET CHECK_SELECTED='TRUE' WHERE GUEST_ID="+gID+";");
                    }
                    	
                    else {
                    	payMethodType=1; //change to default pay method
                    	localDAO.updateCommand("UPDATE PAYMENT_METHOD SET CASH_SELECTED='TRUE' WHERE GUEST_ID="+gID+";");
                    	localDAO.updateCommand("UPDATE PAYMENT_METHOD SET CREDIT_SELECTED='FALSE' WHERE GUEST_ID="+gID+";");
                    	localDAO.updateCommand("UPDATE PAYMENT_METHOD SET DEBIT_SELECTED='FALSE' WHERE GUEST_ID="+gID+";");
                    	localDAO.updateCommand("UPDATE PAYMENT_METHOD SET CHECK_SELECTED='FALSE' WHERE GUEST_ID="+gID+";");
                    }
                    	
                }
            }
        }


        JRadioButton r1 = new JRadioButton("Pay by Cash");
        r1.setActionCommand("cash");

        JRadioButton r2 = new JRadioButton("Pay by Credit Card");
        r2.setActionCommand("credit");

        JRadioButton r3 = new JRadioButton("Pay by Debit Card");
        r3.setActionCommand("debit");

        JRadioButton r4 = new JRadioButton("Pay by Check");
        r4.setActionCommand("check");

        // add event handler
        MyItemListener myItemListener = new MyItemListener();
        r1.addItemListener(myItemListener);
        r2.addItemListener(myItemListener);
        r3.addItemListener(myItemListener);
        r4.addItemListener(myItemListener);

        // add radio buttons to a ButtonGroup
        final ButtonGroup group = new ButtonGroup();
        group.add(r1);
        group.add(r2);
        group.add(r3);
        group.add(r4);

        // Frame setting
        //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        dialog.setSize(300, 200);
        Container cont = dialog.getContentPane();

        cont.setLayout(new GridLayout(0, 1));
        cont.add(new JLabel("Please Choose the Payment Method:"));
        cont.add(r1);
        cont.add(r2);
        cont.add(r3);
        cont.add(r4);

        dialog.setVisible(true);
        dialog.setTitle("Payment Method");
    }
    
    public String getType(){
    	return type;
    }
}

